package products

import (
	"fmt"

	"log"
)

//所有跟products表有关的操作 都在这里

//定义表结构(应该跟需要的数据有关:而不是完整的针对数据表)
//封装相关的操作函数(结构方法也可以)

type IndexProduct struct {
	//首页需要显示的数据   商品ID  品牌名字  系列名字  商品名字  商品价格 商品销量 商品图片 商品类别 是否特价

	Product_id       int     `gorm:"primarykey"`
	Style            string  `gorm:size:10"`
	Price            float64 `gorm:size:""`
	Is_special_offer int     `gorm:size:""`
	Special_price    float64 `gorm:size:""`
	Image            string  `gorm:size:100"`
	Gender           string  `gorm:size:10"`
	Ordered_num      int     `gorm:size:"`

	//品牌信息

	Chinese_name string `gorm:"size:100"`
	English_name string `gorm:size:100"`
	Grade        string `gorm:size:2"`

	//系列信息
	Series_name string `gorm:"column:series_name;size:100"`
}

// 特价商品结构
type SpecialProduct struct {
	//首页需要显示的数据   商品ID  品牌名字  系列名字  商品名字  商品价格 商品销量 商品图片 商品类别 是否特价

	Product_id       int     `gorm:"primarykey"`
	Style            string  `gorm:size:10"`
	Price            float64 `gorm:size:""`
	Is_special_offer int     `gorm:size:""`
	Special_price    float64 `gorm:size:""`
	Image            string  `gorm:size:100"`
	Ordered_num      int     `gorm:size:"`

	//品牌信息

	Chinese_name string `gorm:"size:100"`
	English_name string `gorm:size:100"`

	//系列信息
	Series_name string `gorm:"column:series_name;size:100"`
}

//获取首页所需要的数据(连表操作)

func GetIndexProducts() []IndexProduct {

	//定义切片保存结果S
	var products []IndexProduct
	//需要用到数据库操作

	database.Gdb.Raw("select p.product_id,p.image,p.price,p.is_special_offer,p.special_price,p.gender,p.style,p.ordered_num,s.series_name,b.chinese_name,b.grade from products p left join series s on p.series_id = s.series_id left join brands b on s.brand_id = b.brand_id;").Scan(&products)

	return products

}

// 获取总记录数
// 定义 Count 结构体
type Count struct {
	Count int `gorm:"column:c"`
}

// 获取指定性别的产品数量
func GetGenderCounts(gender string) int {
	var count Count
	// 使用参数化查询，防止 SQL 注入
	err := database.Gdb.Raw("SELECT count(*) AS c FROM products WHERE gender = ?", gender).Scan(&count).Error
	if err != nil {
		log.Fatalf("查询出错: %v", err)
	}
	fmt.Println(count)
	return count.Count
}

func GetSpecialCounts() int {
	var count int
	database.Gdb.Raw("select count(*) count from products where is_special_offer = 1").First(&count)
	return count
}

// 获取所有的特价商品
func GetSpecialProducts(page int, pagecount int) []IndexProduct {
	//定义变量保存数据
	var indexProducts []IndexProduct

	//计算起始位置
	start := (page - 1) * pagecount

	//查询数据库
	database.Gdb.Raw(fmt.Sprintf("select p.product_id,p.style,p.price,p.is_special_offer,p.special_price,p.image,p.ordered_num,s.series_name,b.chinese_name,b.english_name from products p left join series s on p.series_id = s.series_id left join brands b on s.brand_id = b.brand_id where p.is_special_offer = 1 limit %d,%d", start, pagecount)).Scan(&indexProducts)

	return indexProducts
}

// 获取所有男士商品
func GetGenderProducts(gender string, page int, pagecount int) []IndexProduct {
	//将条件组装到SQL执行

	var products []IndexProduct

	//分页显示数据的逻辑:limit 起始位置 限制位置
	start := (page - 1) * pagecount

	//SQL执行
	database.Gdb.Raw(fmt.Sprintf("select p.product_id,p.image,p.price,p.is_special_offer,p.special_price,p.gender,p.style,p.ordered_num,s.series_name,b.chinese_name,b.grade from products p left join series s on p.series_id = s.series_id left join brands b on s.brand_id = b.brand_id where p.gender = '%s' order by p.price desc limit %d,%d", gender, start, pagecount)).Scan(&products)
	return products
}

//获取新品数据:限定4条

func GetNewProducts() []IndexProduct {

	var newProducts []IndexProduct

	database.Gdb.Raw("select p.product_id,p.image,p.price,p.is_special_offer,p.special_price,p.gender,p.style,p.ordered_num,s.series_name,b.chinese_name,b.grade from products p left join series s on p.series_id = s.series_id left join brands b on s.brand_id = b.brand_id where p.is_new = 1 limit 4").Scan(&newProducts)

	return newProducts
}

type ProductDetail struct {
	Product_id       int     `gorm:"primary"`
	Series_id        int     `gorm:""`
	Case_material    string  `gorm:"size:10"`
	Case_back        string  `gorm:"size:10"`
	Strap_color      string  `gorm:"size:10"`
	Functions        string  `gorm:""`
	Style            string  `gorm:"size:10"`
	Size             string  `gorm:"size:20"`
	Watch_glass      string  `gorm:"size:30"`
	Watch_buckle     string  `gorm:"size:10"`
	Launch_year      string  `gorm:"size:4"`
	Movement         string  `gorm:"size:10"`
	Thickness        float64 `gorm:""`
	Dial             string  `gorm:"size:10"`
	Water_resistance string  `gorm:"size:50"`
	Price            float64 `gorm:""`
	Is_new           bool    `gorm:""`
	Is_special_offer bool    `gorm:""`
	Special_price    float64 `gorm:""`
	Image            string  `gorm:"size:100"`
	Gender           string  `gorm:"size:10"`
	Ordered_num      int     `gorm:""`

	//系列名字
	Series_name string `gorm:"size:100"`

	//品牌  名字 + 级别
	Brand_id     int    `gorm:""`
	Chinese_name string `gorm:"size:100"`
	English_name string `gorm:"size:100"`
	Grade        string `gorm:"size:100"`
}

func GetDetail(id string) ProductDetail {
	var product ProductDetail

	database.Gdb.Raw("select p.*,s.series_name,b.chinese_name,b.english_name,b.grade from products p left join series s on p.series_id = s.series_id left join brands b on s.brand_id = b.brand_id where p.product_id = \"" + id + "\"").Scan(&product)

	return product
}

// 获取品牌对应的热销商品
// 获取品牌对应的热销商品
func GetBrandProduct(brand_id int) []IndexProduct {
	var products []IndexProduct

	database.Gdb.Raw(fmt.Sprintf("select p.*,s.series_name,b.chinese_name,b.brand_id,b.english_name,b.grade from products p left join series s on p.series_id = s.series_id left join brands b on s.brand_id = b.brand_id where b.brand_id = %d order by p.ordered_num desc limit 3", brand_id)).Scan(&products)
	return products
}
